<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $username
 * @property string $firstname
 * @property string $lastname
 * @property string $password
 * @property string $salt
 * @property string $birth_date
 * @property string $mobile_phone
 * @property string $gender
 * @property string $id_card
 * @property string $passport_no
 * @property string $facebook_no
 * @property string $status
 * @property string $profile_photo
 * @property integer $nationality_id
 * @property string $created_date
 * @property string $updated_date
 * @property integer $updated_user_id
 *
 * The followings are the available model relations:
 * @property DocumentOfUser[] $documentOfUsers
 * @property ProjectOfUser[] $projectOfUsers
 * @property ProjectOfUser[] $projectOfUsers1
 * @property Nationality $nationality
 * @property UserAddress[] $userAddresses
 * @property UserEducation[] $userEducations
 */
class User extends CActiveRecord
{

    public $rePassword;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, firstname_en, lastname_en, password, rePassword, mobile_phone, gender', 'required'),
			array('religion_id, nationality_id, updated_user_id', 'numerical', 'integerOnly'=>true),
			array('username', 'length', 'max'=>60),
			array('nickname', 'length', 'max'=>30),
			array('username', 'unique'),
			array('firstname_en, lastname_en, firstname_th, lastname_th, id_card, profile_photo', 'length', 'max'=>200),
			array('password, mobile_phone, facebook_no, rePassword', 'length', 'max'=>45),
			array('salt', 'length', 'max'=>5),
			array('gender, status', 'length', 'max'=>1),
			array('passport_no', 'length', 'max'=>10),
			array('birth_date, created_date, updated_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, firstname_en, lastname_en, firstname_th, lastname_en, nickname, password, rePassword, salt, birth_date, mobile_phone, gender, id_card, passport_no, facebook_no, status, profile_photo, nationality_id, created_date, updated_date, updated_user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'documentOfUsers' => array(self::HAS_MANY, 'DocumentOfUser', 'user_id'),
			'projectOfUsers' => array(self::HAS_MANY, 'ProjectOfUser', 'user_id'),
			'projectOfUsers1' => array(self::HAS_MANY, 'ProjectOfUser', 'updated_user_id'),
			'religion' => array(self::BELONGS_TO, 'Religion', 'religion_id'),
		    'nationality' => array(self::BELONGS_TO, 'Nationality', 'nationality_id'),
			'userAddresses' => array(self::HAS_MANY, 'UserAddress', 'user_id'),
			'userEducations' => array(self::HAS_MANY, 'UserEducation', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Email',
			'firstname_en' => 'Firstname',
			'lastname_en' => 'Lastname',
			'firstname_th' => 'Firstname (Th)',
			'lastname_th' => 'Lastname (Th)',
			'nickname' => 'Nickname',
			'password' => 'Password',
			'rePassword' => 'Repeat Password',
			'salt' => 'Salt',
			'birth_date' => 'Birth Date',
			'mobile_phone' => 'Mobile Phone',
			'gender' => 'Gender',
			'id_card' => 'ID Card',
			'passport_no' => 'Passport No',
			'facebook_no' => 'Facebook No',
			'status' => 'Status',
			'profile_photo' => 'Profile Photo',
			'nationality_id' => 'Nationality',
			'created_date' => 'Created Date',
			'updated_date' => 'Updated Date',
			'updated_user_id' => 'Updated User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('firstname',$this->firstname,true);
		$criteria->compare('lastname',$this->lastname,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('salt',$this->salt,true);
		$criteria->compare('birth_date',$this->birth_date,true);
		$criteria->compare('mobile_phone',$this->mobile_phone,true);
		$criteria->compare('gender',$this->gender,true);
		$criteria->compare('id_card',$this->id_card,true);
		$criteria->compare('passport_no',$this->passport_no,true);
		$criteria->compare('facebook_no',$this->facebook_no,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('profile_photo',$this->profile_photo,true);
		$criteria->compare('nationality_id',$this->nationality_id);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('updated_date',$this->updated_date,true);
		$criteria->compare('updated_user_id',$this->updated_user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave()
    {
        if($this->isNewRecord)
        {
            $this->salt = rand(10000, 99999); // random number 5 digit
            $this->password = $this->hashPassword($this->password, $this->salt);
            $this->created_date = new CDbExpression('NOW()');
        }
        return parent::beforeSave();
    }

    protected function afterFind ()
    {
            // convert to display format
        $this->birth_date = strtotime ($this->birth_date);
        $this->birth_date = date ('d/m/Y', $this->birth_date);

        parent::afterFind ();
    }

    protected function beforeValidate ()
    {
        // convert to storage format
        $this->birth_date = strtotime($this->birth_date);
        $this->birth_date = date('Y-m-d', $this->birth_date);

        return parent::beforeValidate ();
    }

    public function validatePassword($password)  {
      return $this->hashPassword($password,$this->salt)===$this->password;
    }
    public function hashPassword($password,$salt) {
      return md5($salt.$password);
    }
}